#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[200005],pre[200005],la[1000005];
int val[200005],dp[200005],mx[200005];
int n;
struct BIT{
    int b[800005];
    void clr()
    {
        memset(b,0,sizeof(b));
    }
    void upd(int x,int a)
    {
        for(;x<=n;x+=(x&(-x)))
            b[x]=max(b[x],a);
    }
    int qry(int x)
    {
        int res=0;
        for(;x;x-=(x&(-x)))
            res=max(res,b[x]);
        return res;
    }
}bit;
signed main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T;
    cin>>T;
    while(T--){
        cin>>n;
        bit.clr();
        memset(la,0,sizeof(la));
        for(int i=1;i<=n;i++)
            cin>>a[i],pre[i]=la[a[i]],la[a[i]]=i;
        for(int i=1;i<=n;i++)
            val[i]=val[i-1]+(a[i]==a[i-1])*a[i];
        memset(dp,0,sizeof(dp));
        memset(mx,0,sizeof(mx));
        for(int i=2;i<=n;i++)
        {
            /*for(int j=1;j<i-1;j++)
            {
                dp[i]=max(dp[i],mx[j+1]+val[i-1]-val[j+1]+(a[i]==a[j])*a[i]);
                mx[i]=max(mx[i],dp[i]);
            }
            dp[i]=max(dp[i],dp[i-1]+(a[i]==a[i-1])*a[i]);*/
            dp[i]=mx[i]=bit.qry(i-1)+val[i-1];
            if(pre[i]==i-1)
                dp[i]=max(dp[i],dp[i-1]+a[i]);
            else if(pre[i]!=0)
                dp[i]=mx[i]=max(mx[i],mx[pre[i]+1]-val[pre[i]+1]+val[i-1]+a[i]);
            bit.upd(i,mx[i]-val[i]);
        }
        cout<<dp[n]<<'\n';
    }
    return 0;
}
